package com.lkx.web;

import com.lkx.domain.Order;
import com.lkx.domain.TblUserNoPart;
import com.lkx.domain.TblUserPart;
import com.lkx.service.TblUserNoPartService;
import com.lkx.service.TblUserPartService;
import com.lkx.util.RandomStringUtil;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * ClassName: UserNotPartController
 * Function:  TODO
 * Date:      2020/2/24 22:08
 * author     likaixuan
 * version    V1.0
 */
@RestController
public class UserNotPartController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);


    /**使用RocketMq的生产者*/
    @Autowired
    private DefaultMQProducer defaultMQProducer;

    @Autowired
    private TblUserNoPartService tblUserNoPartService;

    @Autowired
    private TblUserPartService tblUserPartService;



    @GetMapping("/addUserNotPart")
    @Transactional
    public int addUser(){
        int num = 10000;
        long startTime = new Date().getTime();
        List<TblUserNoPart> list = new ArrayList<>();
        TblUserNoPart userNotPart = null;
        Random random = null;
        for(int i=0;i<num;i++) {
            random = new Random();
            userNotPart = new TblUserNoPart();
            userNotPart.setAge(random.nextInt(100));
            userNotPart.setCreateTime(new Date());
            userNotPart.setEmail(RandomStringUtil.create(10)+"@qq.com");
            userNotPart.setUsername(RandomStringUtil.create(8));
            userNotPart.setType(8);
            list.add(userNotPart);
            tblUserNoPartService.insert(userNotPart);
        }
        long endTime = new Date().getTime();
        logger.info(num+" addlist共用时:"+(endTime-startTime)+" ms");
        return num;
    }

    @GetMapping("/addUserPart")
    @Transactional
    public int addUserPart(){
        int num = 10000;
        long startTime = new Date().getTime();
        List<TblUserPart> list = new ArrayList<>();
        TblUserPart userPart = null;
        Random random = null;

        int id = 5100002;
        for(int i=0;i<num;i++) {
            random = new Random();
            userPart = new TblUserPart();
            userPart.setId(id);
            userPart.setAge(random.nextInt(100));
            userPart.setCreateTime(new Date());
            userPart.setEmail(RandomStringUtil.create(10)+"@qq.com");
            userPart.setUsername(RandomStringUtil.create(8));
            userPart.setType(8);
            list.add(userPart);
            tblUserPartService.insert(userPart);
            id++;
        }
        long endTime = new Date().getTime();
        logger.info(num+" addlist共用时:"+(endTime-startTime)+" ms");
        return num;
    }
}
